import createUtil from '../../libs/util.js';
const util = createUtil();

import routeIndex1 from '../../router/index-1.js';
import routeIndex2 from '../../router/index-2.js';
import routeIndex3 from '../../router/index-3.js';
const routesArray = routeIndex1.concat(routeIndex2, routeIndex3);

const state = {
    user: {
        token: '',
        shopMode: '',
        oda: {},
        id:'',
        goods_id:''
    },
    navigation: [],
    notice: {
        show: true
    }
};

const getters = {
    getUserDataBySessionStorage() {
        var user = window.sessionStorage.getItem('user') || false;
        if(user) {
            user = JSON.parse(user);
        }
        return user;
    }
};

const mutations = {
    setElementValue(state, params) {
        // console.log(params);

        if(params instanceof Array) {
            for(let x of params) {
                let { key, value } = x,
                    k = key.split('.');
                
                switch(k.length) {
                    case 2:
                        state[k[0]][k[1]] = value;
                        break;
                    case 3:
                        state[k[0]][k[1]][k[2]] = value;
                        break;
                    default:
                        state[k[0]] = value;
                        break;
                }
            }
        } else {
            let { key, value } = params;
            state[key] = value;
        }
    },
    buildRouterDataset(state) {
        var keys = {}, children = [];

        for(let x of routesArray) {
            if(!x.meta) continue;

            if(!x.meta.menu.parent) {
                state.navigation.push({
                    index: x.name,
                    path: x.path,
                    name: x.meta.menu.name,
                    icon: x.meta.menu.icon,
                    children: []
                });
                keys[x.name] = state.navigation.length - 1;
            }else {
                children.push(x);
            }
        }

        for(let x of children) {
            let index = keys[x.meta.menu.parent];
            state.navigation[index].children.push({
                index: x.name,
                path: x.path,
                name: x.meta.menu.name,
                icon: x.meta.menu.icon,
                image: x.meta.menu.image
            });
        }
    },
    initializeUserDataset(state, params) {
        if(params.data) {
            state.user.token = params.data.authKey;
            state.user.shopMode = Boolean(Number(params.data.retrospect_model)) ? 'scan' : 'all';
            state.user.oda = params.data.userInfo;
        } else {
            state.user = params;
        }

        window.sessionStorage.setItem('user', JSON.stringify(state.user));
    },
    // setGoodsId(state,params){
    //     params.id = state.user.goods_id
    // }
};

const actions = {
    checkUserIdentity({ getters, commit }, params) {
        var user = getters.getUserDataBySessionStorage;
        console.log(user)
        if(user) {
            commit('initializeUserDataset', user);
            commit('shop/settingShopMode', user.shopMode, { root: true });

            if(params.success) params.success(user);
        }else {
            if(params.callBack) params.callBack(user);
        }
    },
    userLoadingOperate({ commit }, params) {
        util.postRequestInterface(params.url, params.data, (res) => {
            console.log(res)
            if(res.data.userInfo) {
                commit('initializeUserDataset', res);

                let mode = Boolean(Number(res.data.retrospect_model)) ? 'scan' : 'all';
                commit('shop/settingShopMode', mode, { root: true });
            }

            if(params.callBack) params.callBack(res);
        });
    }
};

export default {
	namespaced: true,
	state,
	getters,
    mutations,
    actions
};
